package simplelab;

//jcanvas
import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.Graphics;
import java.awt.Canvas;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;

public class DebugMap {

    private static World world;
    public static Integer[][] data;
    private static JFrame frame;
    private static Position pos;
    private static int dir;
    private static JTable table;
    public static MyCanvas mcanvas;
    public Position lastSize = new Position(World.worldWidth, World.worldHeight);
    boolean every10Line = true;
    private int needAnotherClean = 0;
    
    public DebugMap(World w) {
        System.out.println("egy új debug");
        world = w;
        table = new JTable();
        frame = new JFrame("Debug");
        frame.setSize(World.MaxMapSize * 8 + 40, World.MaxMapSize * 8 + 40);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        mcanvas = new MyCanvas();
        frame.getContentPane().add(mcanvas);
        frame.setVisible(true);

    }
    

    public class MyCanvas extends Canvas {


        public MyCanvas() {
        }

        //kirajzolásért felelős a az ágens pozicóját és a kaját helyzetét kapja meg
        public void paint(Graphics graphics, ArrayList<Food> Foods) {
            /*if(!world.macroblocks){
            data = new Integer[World.worldWidth][World.worldHeight];
            for(int i = 0; i < World.worldWidth; ++i){
            
            for(int l = 0; l < World.worldHeight; ++l){
            // a mező értékének megfelelő szinárnyalatúként tölti fel
            graphics.setColor(new Color(255-world.tile[i][l].value*50,255-world.tile[i][l].value*50,255-world.tile[i][l].value*50));
            graphics.fillRect(i*8+1, l*8+1, 7, 7);
            } 
            //vonalak megrajzolása
            graphics.setColor(Color.black);
            //graphics.drawLine(0, i*8, World.worldWidth*8, i*8);	
            graphics.drawLine(i*8, 0, i*8, World.worldHeight*8);	
            }
            // az utolsó sort is be kell vonalazni
            graphics.drawLine(World.worldHeight*8, 0, World.worldHeight*8, World.worldHeight*8);
            graphics.setColor(Color.black);
            for(int i = 0; i < World.worldWidth+1; ++i){		
            graphics.drawLine(0, i*8, World.worldWidth*8, i*8);	
            }
            
            int plus = 1;
            if(dir == 0 || dir == 3) plus = -1;
            
            //látottak kikékítése
            graphics.setColor(new Color(25,25,220));
            int tempX, tempY;
            for(int i = 1; i < 11; ++i){
            for(int l = -1 * i; l <= i; ++l){
            if(dir % 2 == 0){
            tempX = pos.x + l;
            tempY = pos.y + plus * i;
            }else{
            tempX = pos.x + plus * i;
            tempY = pos.y + l;
            }
            if(tempX > -1 && tempX < World.worldWidth && tempY > -1 && tempY < World.worldHeight )
            graphics.fillRect(tempX*8+1, tempY*8+1, 7, 7);
            }
            }
            }else{*/
            
            
            try{
            if(needAnotherClean != 0 && needAnotherClean < World.Friendlies.size()+ 1){
                graphics.clearRect(0, 0, World.MaxMapSize * 8 + 40, World.MaxMapSize * 8 + 40);
                needAnotherClean++;
            }else{
                needAnotherClean =0;
            }
            if(!lastSize.equals(new Position(World.worldWidth, World.worldHeight))){
                lastSize = new Position(World.worldWidth, World.worldHeight);
                //graphics.clearRect(0, 0, World.MaxMapSize * 8 + 40, World.MaxMapSize * 8 + 40);
                needAnotherClean++;
            }

//            data = new Integer[World.worldWidth / World.macro][World.worldHeight / World.macro];
            for (int i = 0; i < (World.worldWidth) / World.macro; i++) {

                for (int l = 0; l < (World.worldHeight) / World.macro; l++) {
                    // a mező értékének megfelelő szinárnyalatúként tölti fel
                    graphics.setColor(new Color(255 - world.macroblock[i][l].value * 50, 255 - world.macroblock[i][l].value * 50, 255 - world.macroblock[i][l].value * 50));
                    graphics.fillRect(i * 8 * World.macro + 1, l * 8 * World.macro + 1, World.macro * 8 - 1, World.macro * 8 - 1);
                }
                //vonalak megrajzolása
                graphics.setColor(Color.black);
                //graphics.drawLine(0, i*8, World.worldWidth*8, i*8);	
                if(every10Line && i % 10 == 0){
                    graphics.setColor(Color.yellow);
                        graphics.drawLine(i * 8 * World.macro, 0, i * 8 * World.macro, World.worldHeight * 8 * World.macro);
                        graphics.setColor(Color.black);
                    }else
                
                graphics.drawLine(i * 8 * World.macro, 0, i * 8 * World.macro, World.worldHeight * 8 * World.macro);
            }
            // az utolsó sort is be kell vonalazni
            graphics.drawLine(World.worldWidth * 8 * World.macro, 0, World.worldWidth * 8 * World.macro, World.worldHeight * 8 * World.macro);
            graphics.setColor(Color.black);
            for (int i = 0; i < World.worldHeight / World.macro + 1; ++i) {
                if(every10Line && i % 10 == 0){
                    graphics.setColor(Color.yellow);
                    graphics.drawLine(0, i * 8 * World.macro, World.worldWidth * 8 * World.macro, i * 8 * World.macro);
                    graphics.setColor(Color.black);
                }else
                    graphics.drawLine(0, i * 8 * World.macro, World.worldWidth * 8 * World.macro, i * 8 * World.macro);
                
            }

            for (int k = 0; k < World.Friendlies.size(); k++) {
                dir = World.Friendlies.get(k).direction;
                pos = World.Friendlies.get(k).position;
                int plus = 1;
                if (dir == 0 || dir == 3) {
                    plus = -1;
                }

                //látottak kikékítése
                graphics.setColor(new Color(25, 25, 220));
                int tempX, tempY;
                for (int i = 1; i < 11; ++i) {
                    for (int l = -1 * i; l <= i; ++l) {
                        if (dir % 2 == 0) {
                            tempX = pos.x + l;
                            tempY = pos.y + plus * i;
                        } else {
                            tempX = pos.x + plus * i;
                            tempY = pos.y + l;
                        }
                        if (tempX > -1 && tempX < World.worldWidth && tempY > -1 && tempY < World.worldHeight) {
                            graphics.fillRect(tempX * 8 + 1, tempY * 8 + 1, 7, 7);
                        }
                    }
                }
            }
            //}

            //kaja pirossal
            graphics.setColor(Color.red);
            for (int i = 0; i < Foods.size(); i++) {
                graphics.fillRect(Foods.get(i).position.x * 8 + 1, Foods.get(i).position.y * 8 + 1, 7, 7);

            }
            for (int k = 0; k < World.Friendlies.size(); k++) {
                pos = World.Friendlies.get(k).position;
                graphics.setColor(Color.green);
                graphics.fillRect(pos.x * 8 + 1, pos.y * 8 + 1, 7, 7);
                
                //this.paint(graphics);
			/* We would be using this method only for the sake
                 * of brevity throughout the current section. Note
                 * that the Graphics class has been acquired along
                 * with the method that we overrode. */
            }
            for(int l = 0; l < World.Hostiles.size(); l++){
                pos = World.Hostiles.get(l).position;
                graphics.setColor(Color.CYAN);
                graphics.fillRect(pos.x * 8 + 1, pos.y * 8 + 1, 7, 7);
            }
            }catch (ArrayIndexOutOfBoundsException e){  
                e.printStackTrace();
            }catch (Throwable e){
                e.printStackTrace();
            }
        }
    }
}